import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class TrackModel {

	TrackModelPane pane;
	CTCGUI ctc;
	public Block[] builder;
	
	public TrackModel(CTCGUI ctc)
	{

		this.ctc = ctc;
		pane = new TrackModelPane();
		new TrainModel(220000);

	}

	public int getNumSections()
	{
		return 4;
	}
	
	public void build(String filename) throws IOException{
		int number, speedLimit, type;
		double length, grade;
		boolean underground = false;
		String st;
		String row;
		String parsedRow[] = new String[6];
		builder = new Block[150];

		BufferedReader file = new BufferedReader(new FileReader("red.csv"));
		row = file.readLine();

		builder[0] = new Block(0, 1, 0, 55, false, 0);
		int i = 1;
		while(row != null){
			parsedRow = row.split(",", 7);
			//number = Integer.parseInt(parsedRow[0]);
			length = Double.parseDouble(parsedRow[1]);
			grade = Double.parseDouble(parsedRow[2]);
			speedLimit = Integer.parseInt(parsedRow[3]);

			if(parsedRow[4].equals("UNDERGROUND")){
				underground = true;
			}else{
				underground = false;
			}
			if(parsedRow[5].equals("station")){
				type = 1;
			}else if(parsedRow[5].equals("switch")){
				type = 2;
			}else if(parsedRow[5].equals("railcrossing")){
				type = 3;
			}else{
				type = 0;
			}

			if(parsedRow[6] != null)
			{
				st = parsedRow[6];
				builder[i] = new Block(i, length, grade, speedLimit, underground, type, st);
			}else
			{
				builder[i] = new Block(i, length, grade, speedLimit, underground, type);
			}
			row = file.readLine();
			i++;
		}

		file.close();

		for(i = 1; i < 77; i++){

			switch(i){
			case 66:builder[66].prev = builder[65];
			builder[66].next = builder[52];
			break;
			case 67:builder[67].prev = builder[68];
			builder[67].next = builder[43];
			break;
			case 68:builder[68].prev = builder[i+1];
			builder[68].next = builder[i-1];
			break;
			case 69:builder[69].prev = builder[i+1];
			builder[69].next = builder[i-1];
			break;
			case 70:builder[70].prev = builder[i+1];
			builder[70].next = builder[i-1];
			break;
			case 71:builder[71].prev = builder[38];
			builder[71].next = builder[70];
			break;
			case 72:builder[72].prev = builder[73];
			builder[72].next = builder[32];
			break;
			case 73:builder[73].prev = builder[i+1];
			builder[73].next = builder[i-1];
			break;
			case 74:builder[74].prev = builder[i+1];
			builder[74].next = builder[i-1];
			break;
			case 75:builder[75].prev = builder[i+1];
			builder[75].next = builder[i-1];
			break;
			case 76:builder[76].prev = builder[27];
			builder[76].next = builder[75];
			break;
			default:builder[i].prev = builder[i-1];
			builder[i].next = builder[i+1];
			}

			//System.out.println( builder[i].toString() );
			ctc.appending = ctc.appending + "\n" + builder[i].toString();
			ctc.textAreaDisplay.setText(ctc.appending);
			ctc.textAreaDisplay.setCaretPosition(ctc.textAreaDisplay.getDocument().getLength());
		}
	}


	public void receive(TKBundle b) throws InterruptedException
	{
		Thread.sleep(3000);
		pane.status = "Bundle: Track Controller bundle received.";
		System.out.println(pane.status);
		pane.lblstatus.setText(pane.status);
		//update from bundle
		Thread.sleep(3000);

		pane.status = "Bundle: Train Model bundle sent.";
		System.out.println(pane.status);
		pane.lblstatus.setText(pane.status);

		TMBundle testTMBundle = new TMBundle();
		testTMBundle.speed = b.speed;
		TrainModel.sendBundle(testTMBundle);
	}

	public void receive(TMBundle b) throws InterruptedException{
		pane.status = "Bundle: Train Model bundle received.";
		System.out.println(pane.status);
		pane.lblstatus.setText(pane.status);
		//update from bundle
		Thread.sleep(3000);
		//call trackmodel.receive() here
		pane.status = "Bundle: Train Model bundle sent.";
		System.out.println(pane.status);
		pane.lblstatus.setText(pane.status);
	}
}
